#!/home/ole/workspace/1_project/demo/pyproj/lmk/vv/bin/python
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "../")))
from plot.DashServer import DashServer
from plot.SimpleConfig import SimpleConfig

config: SimpleConfig


def update(server):
    line = read_from_stdin(verbose=config.verbose)
    element = try_parse_line(line)
    return element


def main(argv):
    global config
    config = SimpleConfig.parse_opt(argv)
    server: DashServer = DashServer(update_func=update,
                                    update_interval=config.server_interval.value,
                                    rolling_mean=config.rolling_mean.value)
    server.start()
    server.stop()


def try_parse_line(line):
    e = line.split()
    if len(e) == 0:
        return None
    return e


def read_from_stdin(verbose=False):
    line = sys.stdin.readline()
    if not line:
        return ''
    if verbose:
        print("[%05d] %s" % (len(line), line))
    return line


# while True:
#     print(sys.stdin.readline())

if __name__ == "__main__":
    main(sys.argv[1:])
